//package com.ocom.gateway.config.limiter;
//
////import net.sunmonkey.db.model.IpRate;
////import net.sunmonkey.gateway.service.IpRateService;
//
//import com.ocom.common.utils.IPUtils;
//import com.ocom.gateway.config.limiter.MyRedisRateLimiter;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.HttpStatus;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
///**
// * IP限流过滤器
// */
//@Component
//public class IpRateGlobalFilter implements Ordered, GlobalFilter {
//
//    @Autowired
//    private MyRedisRateLimiter myRedisRateLimiter;
//
////    @Autowired
////    private IpRateService ipRateService;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        //获取到调用客户端的IP地址        String ip = exchange.getRequest().getRemoteAddress().getHostName();
//        //从数据库中获取到该IP地址对应的限流参数
////        IpRate ipRate = ipRateService.get(ip);
//        String ip = IPUtils.getHostIp();
//
//        //如果允许同行，没有超过该ip的流量限制
//        if(myRedisRateLimiter.isAllowed("ip:"+ip+":",
//                1,
//                1)){
//            return chain.filter(exchange);
//        }else{
//            exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
//            return exchange.getResponse().setComplete();
//        }
//    }
//
//    @Override
//    public int getOrder() {
//        return 1;
//    }
//}
